home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / CCDBMS.ZIP / DBASE.CPP < prev    next >
C/C++ Source or Header  |  1997-03-18  |  6KB  |  214 lines

  1. // =================================================================
  2. // Dbase.cpp 
  3. // =================================================================
  4. // Harold Kasperink / John Dekker 
  5. // Dr. Dobb's Journal 1997
  6. // =================================================================
  7. // Database class
  8. // =================================================================
  9. #include "dbarray.h"
  10. #include "database.h"
  11. #include "oracle.h"
  12.  
  13. ////////////////////////////////////////////////////////////////////
  14. // CDbase::CDbase
  15. ////////////////////////////////////////////////////////////////////
  16. CDbase::CDbase()
  17. {
  18.     m_bConnected = FALSE;
  19.     m_bContinue = TRUE;
  20.     m_pszUsr = 0;
  21.     m_pszPsswd = 0;
  22.     m_pszDb = 0;
  23.     // Lock mutexes
  24.     m_mtxStartSql.Lock();
  25.     m_mtxEndSql.Lock();
  26. }
  27.  
  28. ////////////////////////////////////////////////////////////////////
  29. // CDbase::~CDbase
  30. ////////////////////////////////////////////////////////////////////
  31. CDbase::~CDbase()
  32. {
  33.     // Set flag to stop
  34.     m_mtxInuse.Lock();
  35.     m_bContinue = FALSE;
  36.     m_pCommand = 0;
  37.  
  38.     // Unlock mutexes
  39.     m_mtxEndSql.Unlock();
  40.     m_mtxStartSql.Unlock();
  41.     
  42.     // Wait until thread is ready
  43.     Join();
  44.     DeleteConnectInfo();
  45.  
  46.     m_mtxStartSql.Unlock();
  47.     m_mtxInuse.Unlock();
  48. }
  49.  
  50. ////////////////////////////////////////////////////////////////////
  51. // CDbase::Do
  52. ////////////////////////////////////////////////////////////////////
  53. // Do Database command
  54. ////////////////////////////////////////////////////////////////////
  55. long CDbase::Do(CDbCommand &command)
  56. {
  57.     // Lock do command
  58.     m_mtxDoCmd.Lock();
  59.     
  60.     // Set command
  61.     m_pCommand = &command;
  62.     
  63.     // Unlock, means thread will perform command
  64.     m_mtxStartSql.Unlock();
  65.  
  66.     // Lock, means thread completed command
  67.     m_mtxEndSql.Lock();
  68.     long lSql = m_lSql;
  69.  
  70.     // Unlock do command
  71.     m_mtxDoCmd.Unlock();
  72.  
  73.     return lSql;
  74. }
  75.  
  76. ////////////////////////////////////////////////////////////////////
  77. // CDbase::Process
  78. ////////////////////////////////////////////////////////////////////
  79. // Process = thread loop
  80. ////////////////////////////////////////////////////////////////////
  81. void CDbase::Process()
  82. {
  83.     while (m_bContinue) {
  84.         // Wait for command
  85.         m_mtxStartSql.Lock();
  86.  
  87.         if (m_pCommand != 0) {
  88.             // Connect if not connected yet
  89.             if (!m_bConnected) 
  90.                 m_lSql = Connect();
  91.         
  92.             // Execute command
  93.             if (m_lSql == 0)
  94.                 m_lSql = m_pCommand->Execute();
  95.  
  96.             // Ready with command execution
  97.             m_mtxEndSql.Unlock();
  98.         }
  99.     }
  100.     Disconnect();
  101. }
  102.  
  103. ////////////////////////////////////////////////////////////////////
  104. // CDbase::Trylock
  105. ////////////////////////////////////////////////////////////////////
  106. //    Check if this object is inuse, and if not 
  107. //    reserve it for caller
  108. ////////////////////////////////////////////////////////////////////
  109. boolean CDbase::TryLock()
  110. {
  111.     if (m_bContinue)
  112.         return m_mtxInuse.TryLock();
  113.  
  114.     return FALSE;
  115. }
  116.  
  117. ////////////////////////////////////////////////////////////////////
  118. // CDbase::Trylock
  119. ////////////////////////////////////////////////////////////////////
  120. //    Set lock
  121. ////////////////////////////////////////////////////////////////////
  122. void CDbase::Lock()
  123. {
  124.     m_mtxInuse.Lock();
  125. }
  126.  
  127. ////////////////////////////////////////////////////////////////////
  128. // CDbase::Unlock
  129. ////////////////////////////////////////////////////////////////////
  130. //    Release lock
  131. ////////////////////////////////////////////////////////////////////
  132. void CDbase::Unlock()
  133. {
  134.     m_mtxInuse.Unlock();
  135. }
  136.  
  137. ////////////////////////////////////////////////////////////////////
  138. // CDbase::ConnectInfo
  139. ////////////////////////////////////////////////////////////////////
  140. // Set connect information strings
  141. ////////////////////////////////////////////////////////////////////
  142. void CDbase::ConnectInfo(const char *szUsr, const char *szPsswd, const char *szDB)
  143. {
  144.     DeleteConnectInfo();
  145.  
  146.     if (szUsr != 0)
  147.         m_pszUsr = strdup(szUsr);
  148.     
  149.     if (szPsswd != 0)
  150.         m_pszPsswd = strdup(szPsswd);
  151.  
  152.     if (szDB != 0)
  153.         m_pszDb = strdup(szDB);
  154. }
  155.  
  156. ////////////////////////////////////////////////////////////////////
  157. // CDbase::DeleteConnectInfo
  158. ////////////////////////////////////////////////////////////////////
  159. // free connect information strings
  160. ////////////////////////////////////////////////////////////////////
  161. void CDbase::DeleteConnectInfo()
  162. {
  163.     if (m_pszUsr != 0)
  164.         delete m_pszUsr;
  165.     
  166.     if (m_pszPsswd != 0)
  167.         delete m_pszPsswd;
  168.     
  169.     if (m_pszDb != 0)
  170.         delete m_pszDb;
  171. }
  172.  
  173. ////////////////////////////////////////////////////////////////////
  174. // CDbase::Connect
  175. ////////////////////////////////////////////////////////////////////
  176. long CDbase::Connect()
  177. {
  178.     if (m_bConnected)
  179.         return 0;
  180.  
  181.     CDbConnect    lConnect(*this, m_pszUsr);
  182.     lConnect.Do();
  183.     
  184.     return 1;
  185. }
  186.  
  187. ////////////////////////////////////////////////////////////////////
  188. // CDbase::Disconnect
  189. ////////////////////////////////////////////////////////////////////
  190. void CDbase::Disconnect()
  191. {
  192.     CDbDisConnect    lDisconnect(*this);
  193.     lDisconnect.Do();
  194. }
  195.  
  196. ////////////////////////////////////////////////////////////////////
  197. // CDbase::Commit
  198. ////////////////////////////////////////////////////////////////////
  199. void CDbase::Commit(boolean bUnlock)
  200. {
  201.     CDbCommit    lCommit(*this, bUnlock);
  202.     lCommit.Do();
  203. }
  204.  
  205. ////////////////////////////////////////////////////////////////////
  206. // CDbase::Rollback
  207. ////////////////////////////////////////////////////////////////////
  208. void CDbase::Rollback(boolean bUnlock)
  209. {
  210.     CDbRollback    lRollback(*this, bUnlock);
  211.     lRollback.Do();
  212. }
  213.  
  214.